<template>
  <div class="topbar">
    <div class="wrapTopbar wrap">
      <div class="mallTick">欢迎来到叩丁狼积分商城</div>
      <ul class="nav">
        <li>
          <img :src="userInfo.headImg" class="pict" alt="" />
        </li>
        <li>用户名：{{ userInfo.nickName }}</li>
        <li>我的积分：{{ userInfo.coin }}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li class="cart-btn" v-if="isLogined">
          <img src="../assets/img/cart.png" alt="" />
          <span>购物车</span>
          <strong>{{ cartTotal }}</strong>
        </li>
        <li class="login" @click="changeIsShowModal(true)" v-else>登录</li>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations, mapActions } from "vuex";
import { WxLoginAPI } from "@/request/api";
export default {
  created() {
    // 页面刷新获取code值
    let myCode = this.$route.query.code;
    // console.log(this.$route.query.code);
    // 如果没有code值就就停止
    if (!myCode) {
      let mytoken = localStorage.getItem("x-auth-token");
      if (mytoken) {
        this.changeIsLogined(Boolean(mytoken));
        this.asyncUserInfo();
      } else {
        this.changeIsLogined(Boolean(mytoken));
        this.initUserInfo();
      }
      return;
    }
    // 有code值执行逻辑
    let res = WxLoginAPI({
      code: myCode,
    }).then((res) => {
      if (res.code == 0) {
        // 成功请求
        this.asyncChangeIsShowToast({
          isShow: true,
          msg: "登录成功",
          type: "success",
        });
        // 保存token值
        localStorage.setItem("x-auth-token", res["x-auth-token"]);
        // 改变登录状态
        this.changeIsLogined(true);
        // 登录成功之后,初始化code
        this.$router.push(this.$route.path);
        // 显示用户信息
        this.asyncUserInfo();
      } else if (res.code == 400) {
        // code失效情况
        // 1. 提醒用户重新扫描
        this.asyncChangeIsShowToast({
          isShow: true,
          msg: "重新扫描二维码",
          type: "warning",
        });
        // 2. 模态框重新显示
        this.changeIsShowModal(true);
      } else if (res.code == 407) {
        // 微信还没有在这个网站绑定过手机,提醒用户先用手机号登录
        // 1. 提醒用户重新扫描
        this.asyncChangeIsShowToast({
          isShow: true,
          msg: "请先用手机登录",
          type: "warning",
        });
        //2. 把登录模态框打开
        this.changeIsShowModal(true);
        //3. 存储uuid （登录时取）
        localStorage.setItem("uuid", res.uuid);
      }
    });
  },
  data() {
    return {
      // cartTotal: 5,
    };
  },
  computed: {
    ...mapState({
      isLogined: (state) => state.loginStatus.isLogined,
      isShowToast: (state) => state.showToast.isShowToast,
      cartTotal: (state) => state.userInfo.cartTotal,
      userInfo: (state) => state.userInfo.userInfo,
    }),
  },
  watch: {
    // 切换路由更新登录状态:方式一:监听路由路径
    // "$route.path": {
    //   handler(newVal, oldVal) {
    //     console.log(newVal, oldVal);
    //     let mytoken = localStorage.getItem("x-auth-token");
    //     // if (mytokenc) {
    //     //   // 有token值登录状态为:登录
    //     //   this.changeIsLogined(true);
    //     // } else {
    //     //   // 没有token值就是未登录状态
    //     //   this.changeIsLogined(false);
    //     // }
    //     this.changeIsLogined(Boolean(mytoken));
    //   },
    // },
  },
  methods: {
    ...mapMutations({
      changeIsShowModal: "showModal/changeIsShowModal",
      changeIsShowToast: "showToast/changeIsShowToast",
      changeIsLogined: "loginStatus/changeIsLogined",
      initUserInfo: "userInfo/initUserInfo",
    }),
    ...mapActions({
      asyncChangeIsShowToast: "showToast/asyncChangeIsShowToast",
      asyncUserInfo: "userInfo/asyncUserInfo",
    }),
  },
};
</script>

<style lang="less" scoped>
.topbar {
  width: 100%;
  height: 40px;
  background: #242b39;

  .wrapTopbar {
    height: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    div.mallTick {
      width: 155px;
      font-size: 14px;
      font-family: SourceHanSansSC;
      font-weight: 300;
      color: #fffefe;
    }
    ul.nav {
      height: 100%;
      font-size: 14px;
      font-family: SourceHanSansSC;
      font-weight: 300;
      color: #fffefe;
      display: flex;
      justify-content: space-between;

      li {
        padding: 0 10px;
        box-sizing: border-box;
        height: 40px;
        line-height: 40px;
        cursor: pointer;
        .pict {
          width: 23px;
          height: 22px;
          border-radius: 13px;
          vertical-align: middle;
        }
        &.login {
          width: 124px;

          text-align: center;
          background: #0a328e;
          font-size: 14px;
          font-family: SourceHanSansSC;
          font-weight: 300;
          color: #fffefe;
        }
      }
      .cart-btn {
        width: 124px;
        height: 40px;
        background: #0a328e;
        text-align: center;
        img {
          width: 20px;
          height: 19px;
          vertical-align: middle;
        }
        span {
          margin: 0 8px;
        }
        strong {
          width: 22px;
          height: 22px;
          line-height: 22px;
          display: inline-block;
          border-radius: 50%;
          background-color: #fd604d;
        }
      }
    }
  }
}
</style>
